﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using WebBHXH.Entities;
using System.Web.Script.Serialization;
using System.IO;

namespace WebBHXH
{
    public partial class ReportCard : System.Web.UI.Page
    {
        List<CoSoKCBModel> listCoSoKCBModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            GetCoSoKCB();
            GetData();
        }

        private static System.Data.DataTable getDataTable()
        {
            System.Data.DataTable myTable = new DataTable("myTable");
            System.Data.DataColumn column;


            //create 3 columns
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "MaSoThe";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "DonViBH_STT";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "Ma_DotBH";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Ma_MucBH";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Ma_NSD";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "GhiChu";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "HoVaTen";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NgaySinh";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "GioiTinh";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "DiaChi";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Ma_CoSoKCB";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Ma_LoaiBH";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "TuNgay";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "DenNgay";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "So";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Hoten";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Birthday";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Address";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "NoiKCB";
            column.ReadOnly = true;


            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ThoiHan";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            return myTable;
        }

        private void GetData()
        {
            string maThe = Request.QueryString["maThe"];
            string hoTen = Request.QueryString["hoTen"];
            string loaiBH = Request.QueryString["loaiBH"];
            string dotBH = Request.QueryString["dotBH"];
            string mucBH = Request.QueryString["mucBH"];
            System.Net.WebRequest request = System.Net.WebRequest.Create("" + Util.getUrl() + "Home/SearchBHYTTNModel?maThe=" + maThe + "&hoTen=" + hoTen + "&loaiBH=" + loaiBH + "&dotBh=" + dotBH + "&mucBH=" + mucBH);
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            System.Data.DataRow row;
            System.Data.DataTable myTable = getDataTable();
            //get response-stream, and use a streamReader to read the content
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    JavaScriptSerializer jsonserializer = new JavaScriptSerializer();
                    List<MaBHYTTNModel> listMaBHYTTN = jsonserializer.Deserialize<List<MaBHYTTNModel>>(sr.ReadToEnd());
                    foreach (MaBHYTTNModel maBHYTTN in listMaBHYTTN)
                    {
                            row = myTable.NewRow();
                            string[] mst = maBHYTTN.MaSoThe.Split('-');
                            row["MaSoThe"] = mst[0];
                            row["DonViBH_STT"] = mst[1];
                            row["Ma_DotBH"] = mst[2];
                            row["Ma_MucBH"] = mst[3];
                            row["Ma_NSD"] = mst[4];
                            row["GhiChu"] = mst[5];
                            row["HoVaTen"] = maBHYTTN.HoVaTen;
                            row["NgaySinh"] = maBHYTTN.NgaySinh.ToShortDateString();
                            row["GioiTinh"] = "Giới Tính: " + maBHYTTN.GioiTinh;
                            row["DiaChi"] = maBHYTTN.DiaChi;
                            


                            CoSoKCBModel coSoKCB = listCoSoKCBModel.Find(delegate(CoSoKCBModel coSoKCBModel)
                            {
                                return coSoKCBModel.MaCoSoKCB.Equals(maBHYTTN.CoSoKCB, StringComparison.Ordinal);
                            });

                            row["Ma_CoSoKCB"] = coSoKCB.Ten_CoSoKCB;
                            row["Ma_LoaiBH"] = "Mã: " + maBHYTTN.CoSoKCB;
                            row["TuNgay"] = "Từ " + maBHYTTN.TuNgay.ToShortDateString();
                            row["DenNgay"] = " đến "+maBHYTTN.DenNgay.ToShortDateString();


                            row["So"] = "Số: ";
                            row["Hoten"] = "Họ và tên: ";
                            row["Birthday"] = "Ngày Sinh: ";
                            row["Address"] = "Địa Chỉ: ";
                            row["NoiKCB"] = "Nơi DK KCB BĐ: ";
                            row["ThoiHan"] = "Thời Hạn Sử Dụng: ";
                            //add the row of data to the table
                            myTable.Rows.Add(row);
                        }
                }
            }

            ReportDocument reportDocument = new ReportDocument();
            reportDocument.Load(Server.MapPath("CrystalReport4.rpt"));
            reportDocument.SetDataSource(myTable);
            CrystalReportViewer1.ReportSource = reportDocument;
        }

        private void GetCoSoKCB()
        {
            listCoSoKCBModel = new CoSoKCBModel().GetCoSoKCB();
        }
    }
}